package com.android.fragmentdemo
import android.os.Bundle
import androidx.fragment.app.FragmentTransaction
import com.hengfoxwolf.commonbase.base.BaseActivity
import com.android.fragmentdemo.databinding.ActivitySecondBinding
import com.android.fragmentdemo.fragments.CategoryFragment
import com.android.fragmentdemo.fragments.CollectFragment
import com.android.fragmentdemo.fragments.ContactsFragment
import com.android.fragmentdemo.fragments.VideoFragment

class SecondActivity : BaseActivity<ActivitySecondBinding>() {

    private var mVideoFragment: VideoFragment? = null
    private var mCollectFragment: CollectFragment? = null
    private var mCategoryFragment: CategoryFragment? = null
    private var mContactsFragment: ContactsFragment? = null

    private var mCheckedId = -1

    override fun getLayoutId() = R.layout.activity_second

    override fun init(savedInstanceState: Bundle?) {
        mBinding.rgSecondBottomTab.setOnCheckedChangeListener { _, checkId ->
            initTabs(checkId)
        }
        initTabs(R.id.rb_video)
    }


    private fun initTabs(checkedId: Int) {
        val transaction = supportFragmentManager.beginTransaction()
        hideFragments(transaction)
        when (checkedId) {
            R.id.rb_video -> {
                if (R.id.rb_video != mCheckedId) {
                    if (mVideoFragment == null) {
                        mVideoFragment = VideoFragment.newInstance()
                        transaction.add(R.id.fl_second_frame, mVideoFragment!!)
                    } else {
                        transaction.show(mVideoFragment!!)
                    }
                    mCheckedId = R.id.rb_video
                }
            }

            R.id.rb_collect -> {
                if (R.id.rb_collect != mCheckedId) {
                    if (mCollectFragment == null) {
                        mCollectFragment = CollectFragment.newInstance()
                        transaction.add(R.id.fl_second_frame, mCollectFragment!!)
                    } else {
                        transaction.show(mCollectFragment!!)
                    }
                    mCheckedId = R.id.rb_collect
                }
            }

            R.id.rb_category -> {
                if (R.id.rb_category != mCheckedId) {
                    if (mCategoryFragment == null) {
                        mCategoryFragment = CategoryFragment.newInstance()
                        transaction.add(R.id.fl_second_frame, mCategoryFragment!!)
                    } else {
                        transaction.show(mCategoryFragment!!)
                    }
                    mCheckedId = R.id.rb_category
                }
            }

            R.id.rb_contacts -> {
                if (R.id.rb_contacts != mCheckedId) {
                    if (mContactsFragment == null) {
                        mContactsFragment = ContactsFragment.newInstance()
                        transaction.add(R.id.fl_second_frame, mContactsFragment!!)
                    } else {
                        transaction.show(mContactsFragment!!)
                    }
                    mCheckedId = R.id.rb_contacts
                }
            }
        }
        transaction.commit()
    }

    private fun hideFragments(transaction: FragmentTransaction) {
        if (mVideoFragment != null) {
            transaction.hide(mVideoFragment!!)
        }
        if (mCollectFragment != null) {
            transaction.hide(mCollectFragment!!)
        }
        if (mCategoryFragment != null) {
            transaction.hide(mCategoryFragment!!)
        }
        if (mContactsFragment != null) {
            transaction.hide(mContactsFragment!!)
        }
    }
}